import { GET_TICKET_LST } from './constants';

import NetUtil from '../../common/NetUtil';
import config from '../../config/config';

export const getTickets = (token, page) => {
    return dispatch => {
        let url = '/s/ticket/getShopCouponList';
        
        const params = {
          page: page,
          size: 5,
          token: token,
        };
        
        dispatch({
            type: GET_TICKET_LST,
            payload: {
                ticketList: [],
                hasMore: false,
                isLoading: true,
                footText: '正在加载...',
                isRefresh: page == 1
            }
        })
        
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            
            if (res.success) {
                let footText = '';
                let hasMore = false;
                if (res.success) {
                    if (res.data.length < 5) {
                        footText = '加载完成',
                        hasMore = false;
                    } else {
                        footText = '上拉加载更多'
                        hasMore = true;
                    }
                } else {
                    footText = '加载失败，下拉重新加载';
                }
    
                dispatch({
                    type: GET_TICKET_LST,
                    payload: {
                        ticketList: res.data,
                        footText: footText,
                        hasMore: hasMore,
                        isLoading: false,
                        isRefresh: false
                    }
                })   
            } else {
                dispatch({
                    type: GET_TICKET_LST,
                    payload: {
                        ticketList: [],
                        footText: '加载错误',
                        hasMore: false,
                        isLoading: false,
                        isRefresh: false
                    }
                })
            }
        });
    }
};
